import os
from flask import Flask, request, send_file, send_from_directory

app = Flask(__name__)

# app.config['UPLOAD_FOLDER'] = 'uploads/'
# app.config['ALLOWED_EXTENSIONS'] = set(['mlmodel'])

@app.route('/')
def index():
    text = '''<b>upload</b>[POST] upload file, parameter - file<br/>
            <b>download</b>[GET] download file, parameter - file<br/>
            <b>data</b>[GET] download data file, parameter - code<br/>
            '''
    return text


@app.route('/upload', methods=['POST'])
def upload():
    files = request.files.getlist("file[]")
    for f in files:
        path = os.path.join(os.getcwd(), 'uploads/', f.filename)
        f.save(path)
    return 'saved'


@app.route('/download')
def download():
    f = request.args.get('file')
    path = os.path.join(os.getcwd(), 'uploads/')
    return send_from_directory(path, f, as_attachment=True)


@app.route('/data')
def data():
    c = request.args.get('code')
    f = c+'.mlmodel'
    path = os.path.join(os.getcwd(), 'data/')
    return send_from_directory(path, f, as_attachment=True)


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=10045)
